clc;
clear;
close all;
%% source signal
% 设置采样点的频率是1000hz，采样时间是1s
fs = 1000;
T = 1;
t = 0:1/fs:T;
%% 设置信号频率是50hz，幅值是10
X = 10 * sin(2 * pi * 50 * t);
L = floor(length(X) / 2) * 2; % Ensure L is even
X = X(1:L); % Truncate X to have even length
Y = fft(X);
subplot(121);
plot(t, X);
title('Original Signal');
xlabel('Time (s)'); 
ylabel('Amplitude');
grid on;
%% fft分析
% 进行fft变换，计算频率分量
L = floor(length(X) / 2) * 2; % Ensure L is even
X = X(1:L); % Truncate X to have even length
Y = fft(X);
% 计算频率分量的幅值
P2 = abs(Y / L);
% 计算频率分量的幅值
P1 = P2(1:L/2+1);
P1(2:end-1) = 2 * P1(2:end-1);
% 计算频率分量的频率
f = fs * (0:(L/2)) / L;
subplot(122);
plot(f, P1);
title('FFT Result');
xlabel('Frequency (Hz)');
ylabel('Magnitude');